#!/bin/sh


# DAY mode
ir_cut_enable() {
  # Pin_mux
  [ ! -d /sys/class/gpio/gpio$gpio_0 ] && echo "$gpio_0" >/sys/class/gpio/export
  [ ! -d /sys/class/gpio/gpio$gpio_1 ] && echo "$gpio_1" >/sys/class/gpio/export

  # Dir
  echo "out" >/sys/class/gpio/gpio$gpio_0/direction
  echo "out" >/sys/class/gpio/gpio$gpio_1/direction

  # Data, gpio_1: 0, gpio_0: 1  (day mode)
  echo "1" >/sys/class/gpio/gpio$gpio_0/value
  echo "0" >/sys/class/gpio/gpio$gpio_1/value

  # Sleep 1s
  sleep 1

  # Back to original
  echo "0" >/sys/class/gpio/gpio$gpio_0/value
  echo "0" >/sys/class/gpio/gpio$gpio_1/value
}


# NIGHT mode
ir_cut_disable() {
  # Pin_mux
  [ ! -d /sys/class/gpio/gpio$gpio_0 ] && echo "$gpio_0" >/sys/class/gpio/export
  [ ! -d /sys/class/gpio/gpio$gpio_1 ] && echo "$gpio_1" >/sys/class/gpio/export

  # Dir
  echo "out" >/sys/class/gpio/gpio$gpio_0/direction
  echo "out" >/sys/class/gpio/gpio$gpio_1/direction

  # Data, gpio_1: 1, gpio_0: 0  (night mode)
  echo "0" >/sys/class/gpio/gpio$gpio_0/value
  echo "1" >/sys/class/gpio/gpio$gpio_1/value

  # Sleep 1s
  sleep 1

  # Back to original
  echo "0" >/sys/class/gpio/gpio$gpio_0/value
  echo "0" >/sys/class/gpio/gpio$gpio_1/value
}


gpio_0=0
gpio_1=0


if [ $# -lt 2 ]; then
  echo "Usage: ircut_demo <vendor> <mode>"
  echo "For example:"
  echo "NIGHT : ircut_demo anjoy 1"
else
  if [ $1 = "anjoy" ]; then
    gpio_0=78
    gpio_1=79
  elif [ $1 = "brovotech" ]; then
    gpio_0=78
    gpio_1=79
  elif [ $1 = "gospell" ]; then
    gpio_0=78
    gpio_1=79
  elif [ $1 = "uniview" ]; then
    gpio_0=78
    gpio_1=79
  else
    echo -e "Wrong vendor name: $1\nPlease select: anjoy, brovotech, gospell, uniview"
    exit
  fi

  if [ $2 -eq 0 ]; then
    echo "DAY mode, ir_cut on"
    ir_cut_enable >/dev/null
  elif [ $2 -eq 1 ]; then
    echo "NIGHT mode, ir_cut off"
    ir_cut_disable >/dev/null
  else
    echo "Invalid mode, please slect 0 or 1"
  fi
fi
